package com.tran.app.screen

import androidx.annotation.DrawableRes
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box

import androidx.compose.foundation.layout.Column

import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import com.tran.app.R
import com.tran.app.data.PlainData
import com.tran.app.viewmodel.MainViewModel
import kotlinx.coroutines.launch

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun Main(modifier: Modifier,navController: NavController){

    val pagerState = rememberPagerState(pageCount = { 2 })
    val vm:MainViewModel = viewModel()
        HorizontalPager(state = pagerState,modifier =modifier.fillMaxSize()) {page->
            Column(modifier = Modifier.fillMaxSize()) {
                Box(modifier = Modifier
                    .weight(1f)
                    .fillMaxWidth()) {
                    when (page) {
                        0-> {
                            HomeScreen(modifier = modifier)
                        }
                        1 -> {
                            MineScreen(modifier = modifier,navController)
                        }
                    }
                }
                val scale = rememberCoroutineScope()
                WeBottomBar(pagerState.currentPage) {
                    scale.launch {
                        pagerState.animateScrollToPage(it)
                    }
                }
            }
        }
}

@Composable
private fun WeBottomBar(select:Int,onSelectOnchange:(Int) -> Unit ){
    Row(modifier = Modifier.background(Color.White), verticalAlignment = Alignment.Bottom) {
        TabItem(R.drawable.baseline_cabin_24,"主页",
            Modifier
                .weight(1f)
                .clickable {
                    onSelectOnchange(0)
                },
            if(select==0) Color.Green else Color.Black )
        TabItem(R.drawable.baseline_notifications_none_24,"我的",
            Modifier
                .weight(1f)
                .clickable {
                    onSelectOnchange(1)
                },
            if(select==1) Color.Green else Color.Black)
    }
}

@Composable
private fun TabItem(@DrawableRes iconid:Int,title:String,modifier: Modifier,tint:Color){
    Column(modifier = modifier.padding(vertical = 8.dp ), horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Icon(painter = painterResource(id = iconid),
            contentDescription = title, modifier = Modifier
                .padding(8.dp)
                .size(24.dp), tint = tint)
        Text(text = title, fontSize = 11.sp, color = tint)
    }
}



@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ModalBottomSheetDemo(vm: MainViewModel,item:PlainData){
    var showDialog = false
    ModalBottomSheet(onDismissRequest = {  }) {
        Column(modifier = Modifier.fillMaxWidth()) {
            TextButton(onClick = {

            }, modifier = Modifier.fillMaxWidth()) {
                Text(text = "编辑")
            }
            TextButton(onClick = {  }, modifier = Modifier.fillMaxWidth()) {
                Text(text = "删除")
            }
        }
    }
}